package com.itheima.controller;

import com.itheima.ShoppingCartService;
import com.itheima.bean.R;
import com.itheima.bean.ShoppingCart;
import com.itheima.config.ThreadLocalConfig;
import com.itheima.dto.DishDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
   @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/sub")
    public R jiang(@RequestBody Map map){
        String dishId = (String)map.get("dishId");
        String setmealId =(String) map.get("setmealId");
        System.out.println(dishId+"============="+setmealId);
        int row =shoppingCartService.jiang(dishId,setmealId);
        if (row>0){
            return R.success("购物车减成功！");
        }return R.error("减购物车失败！");
    }


@DeleteMapping("/clean")
public R deleteAll(){
    try {
        Long userId = ThreadLocalConfig.get();
        shoppingCartService.deleteAll(userId);
        return R.success("清空成功！");
    } catch (Exception e) {
        e.printStackTrace();
        return R.error("清空失败！");
    }

}



   @GetMapping("/list")
    public R show(){
       Long userId = ThreadLocalConfig.get();
       List<ShoppingCart> list= shoppingCartService.show(userId);
       return R.success(list);
   }
   @PostMapping("/add")
    public R add(@RequestBody ShoppingCart shoppingCart){
       System.out.println("==============================="+ThreadLocalConfig.get());
       shoppingCart.setUserId(ThreadLocalConfig.get());
      int row=shoppingCartService.add(shoppingCart);
    if (row>0){
        return R.success("添加购物车成功！");
    }
       return R.error("添加购物车失败！");
   }


}
